const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      activeTab:0,
      page: 1,//加载页数
      pagenumber: 10,//每页数量
      firstLoading: true,//是否第一次加载
      Loading: false, //"加载更多"是否显示，默认false，隐藏
      LoadingComplete: false, //"加载完毕"是否显示，默认false，隐藏
      hidden: true, //是否显示加载中，默认true,隐藏
      end_page:20,
      scrollHeight:300
  },


// 选择结束时间
 endDateTime(e){
    var that = this
    var start_time = new Date(app.cache_read_startTime()).getTime();
    var end_time = new Date(e.detail.value).getTime();
    var timestamp = Date.parse(new Date());  
    if(app.date_judge(start_time,end_time,timestamp) === false){
        return false;
    }
    that.setData({
      end: e.detail.value,
    })
    app.cache_write_endTime(e.detail.value)

 },
// 选择开始时间
  startDateTime(e) {
    var that = this
    var start_time = new Date(e.detail.value).getTime();
    var end_time = new Date(app.cache_read_endTime()).getTime()
    var timestamp = Date.parse(new Date());  
    if(app.date_judge(start_time,end_time,timestamp) === false){
        return false;
    }
     that.setData({
      start: e.detail.value,
    })
    app.cache_write_startTime(e.detail.value);
  },

    // 查看
  select() {
    var that = this;
    this.start_lower()
    that.onShow()
  },


  // 请求方法
  request() {
    var that = this
    var start = app.cache_read_startTime(); 
    var end = app.cache_read_endTime(); 
    var start_shop = wx.getStorageSync('start_shop_');
    var all_start_shop = wx.getStorageSync('all_start_shop_');
    var type = that.data.activeTab;
    that.setData({
      hidden: false,
      userphone: true,
      start_shop: start_shop
    })
    wx.request({
      url: app.https() + 'index/getTakephotos',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        start_time: start,
        end_time: end,
        shop_id: start_shop.id,
        company_id: all_start_shop.company?all_start_shop.company.id:'',
        item :that.data.end_page,
        type : type,
      },
      success: function (res) {
        if (res.data.resCode == 2001) {
          console.log(res)
              //判断是否有数据，有则取数据
            if (res.data.resData.takephotos.length != 0) {
              let lodingList = [];
              that.data.firstLoading ? lodingList = res.data.resData.takephotos : lodingList = that.data.datas.concat(res.data.resData.takephotos)
              that.setData({
                datas: lodingList,//修改评论数据
                Loading: true,//显示，加载更多
                firstLoading: false,
              })
            } else {
              that.setData({
                LoadingComplete: true, //显示，加载完毕
                Loading: false , //隐藏，加载更多
              })
            }
              that.setData({
                start: start,
                end: end,
                total:res.data.resData.total,
                hidden: true
              })
              // 隐藏导航栏加载框
              wx.hideNavigationBarLoading();
              // 停止下拉动作
              wx.stopPullDownRefresh();
        } else {
          console.log(res)
        }
      }
    })

  },
  //切换
  changeTab(e){
    var that = this;
    var tab = e.currentTarget.dataset.tab;
    that.setData({
      activeTab:Number(tab),
    })
    that.start_lower()
    that.request();
  },
  onReady: function(){
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onReady: function(){
    var that = this;
    let query = wx.createSelectorQuery();
    query.select('.top').boundingClientRect()
    query.exec(function (resb) {
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    scrollHeight: res.windowHeight - resb[0].height
                })
            },
        })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      that.data.datas = [];
      that.request();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.start_lower()
    var that = this;
    that.request();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  loadMore: function () {
      this.scrollLower()
  },

  //初始化
  start_lower:function(){
    this.setData({
      page: 1,//加载页数
      pagenumber: 10,//每页数量
      firstLoading: true,
      Loading: false, //"加载更多"是否显示，默认false，隐藏
      LoadingComplete: false, //"加载完毕"是否显示，默认false，隐藏
      hidden: true, //是否显示加载中，默认true,隐藏
      end_page: 10,
    })
  },
  //滚动到底部触发事件
  scrollLower: function () {
    let that = this;
    if (that.data.Loading && !that.data.LoadingComplete) {
      that.setData({
        page: that.data.page + 1,  //每次触发上拉事件，把页码+1
        firstLoading: false,  //不是第一次加载
        end_page:that.data.end_page + 10,
      });
      that.request();
    }
  }
})